from datetime import timedelta
from pathlib import Path

from qingdong.config.celery_config import *  # noqa
from qingdong.config.email_config import *  # noqa

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = "django-insecure-6n2)-i$=n_#n188&_9y0yawkv$e105(xz-1l_=6p&1a95=zjhf"
USER_CENTER_PASSWORD_SECRET_KEY = "ABCabc123"
TOKEN_EXPIRE_TIME = timedelta(days=7)

DEBUG = False

ALLOWED_HOSTS = ["*"]

INSTALLED_APPS = [
    "simpleui",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "user_center",
    "corsheaders",
    "cms",
    "ali_oss",
    "content_center",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    # 'django.middleware.csrf.CsrfViewMiddleware',
    "corsheaders.middleware.CorsMiddleware",  # 新加
    "django.middleware.common.CommonMiddleware",  # 新加
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "common.middleware.HandleLogicErrorMiddleware",
]

ROOT_URLCONF = "qingdong.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates"],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "qingdong.wsgi.application"

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "HOST": "127.0.0.1",
        "NAME": "qingdong",
        "USER": "root",
        "PASSWORD": "1111",
        "ATOMIC_REQUESTS": True,
    }
}

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/2",
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]

LANGUAGE_CODE = "zh-hans"

TIME_ZONE = "Asia/Shanghai"

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = "/static/"

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "standard": {
            "format": (
                """{"levelname": "%(levelname)s", "asctime": "%(asctime)s", "filename": "%(filename)s", """
                """"funcName": "%(funcName)s","name": "%(name)s", "lineno": "%(lineno)s", "message": %(message)s}"""
            )
        }
    },
    "handlers": {
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
        },
        "log_info": {
            "level": "INFO",
            "class": "logging.StreamHandler",
            "formatter": "standard",
        },
    },
    "loggers": {
        "log": {
            "handlers": ["log_info"],
            "propagate": True,
            "level": "INFO",
        },
        "": {
            "handlers": ["log_info"],
            "propagate": True,
            "level": "INFO",
        },
    },
}

# 跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ()
# 对应的发送的请求的跨域
CORS_ALLOW_METHODS = (
    "DELETE",
    "GET",
    "OPTIONS",
    "PATCH",
    "POST",
    "PUT",
    "VIEW",
)

CORS_ALLOW_HEADERS = (
    "accept",
    "accept-encoding",
    "authorization",
    "content-type",
    "dnt",
    "origin",
    "user-agent",
    "x-csrftoken",
    "x-requested-with",
    "token",
)

OSS_ACCESS_KEY_ID = "LTAI4G3EHshW8DGJbWPXqgsg"
OSS_ACCESS_KEY_SECRET = "U3okejkvG8WqMJLM4uMmasrjKMEqOf"
OSS_ENDPOINT = "oss-cn-shenzhen.aliyuncs.com"
OSS_BUCKET_NAME = "caibo-0520"
